<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" /> 
    <title>Redis源码学习之BIO - 天地维杰网</title>
    <meta name="keywords" content="系统架构,shutdown,不与天斗,Domino,博客,程序员,架构师,笔记,技术,分享,java,Redis">
    
    <meta property="og:title" content="Redis源码学习之BIO">
    <meta property="og:site_name" content="天地维杰网">
    <meta property="og:image" content="/img/author.jpg"> 
    <meta name="title" content="Redis源码学习之BIO - 天地维杰网" />
    <meta name="description" content="BIO顾名思义，`background IO`,是redis中运行的后台IO。 网上千篇一律的说法是redis是单线程单进程。 实际上redis运行过程中并不是严格单进程单线程应用。" />
     
    <link rel="shortcut icon" href="http://www.shutdown.cn/img/favicon.ico" />
    <link rel="apple-touch-icon" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link rel="apple-touch-icon-precomposed" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link href="http://www.shutdown.cn/js/vendor/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/main.css" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/syntax.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post"},
     fancybox: true, 
    motion: true
  };
</script>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-7826003325059020" crossorigin="anonymous"></script>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<div class="container one-collumn sidebar-position-left page-home  ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"> <div class="site-meta  custom-logo ">

  <div class="custom-logo-site-title">
    <a href="http://www.shutdown.cn"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">天地维杰网</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">人如秋鸿来有信，事若春梦了无痕</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
    <ul id="menu" class="menu">
      
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/redis/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-full"></i> <br />Redis
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/java/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-coffee"></i> <br />java
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/linux/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-linux"></i> <br />linux
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/daily/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bug"></i> <br />日常问题
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/spring/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-child"></i> <br />Spring和Springboot
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/mac/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-fire"></i> <br />Mac相关
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/middleware/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-gavel"></i> <br />中间件
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jiagou/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-rocket"></i> <br />架构
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/python/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-ship"></i> <br />python
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/front/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bolt"></i> <br />前端
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jvm/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-balance-scale"></i> <br />jvm
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/c/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-empty"></i> <br />c语言
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/web3/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-web3"></i> <br />web3
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/post/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />归档
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/about/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />关于
          </a>
        </li>
      
      <li class="menu-item menu-item-search">
        <a href="javascript:;" class="popup-trigger"> <i class="menu-item-icon fa fa-search fa-fw"></i> <br /> 搜索</a>
      </li>
    </ul>
    <div class="site-search">
      <div class="popup">
 <span class="search-icon fa fa-search"></span>
 <input type="text" id="local-search-input">
 <div id="local-search-result"></div>
 <span class="popup-btn-close">close</span>
</div>

    </div>
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
<section id="posts" class="posts-expand">
  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <header class="post-header">
      <h1 class="post-title" itemprop="name headline">
        <a class="post-title-link" href="http://www.shutdown.cn/post/redis-source-code-learn-of-bio/" itemprop="url">
        Redis源码学习之BIO
        </a>
      </h1>
      <div class="post-meta">
      <span class="post-time">
<span class="post-meta-item-icon">
    <i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">时间：</span>
<time itemprop="dateCreated" datetime="2016-03-22T13:04:35+08:00" content="2022-10-21">
    2022-10-21
</time>
</span> 
      

  <span class="post-category" >
  &nbsp; | &nbsp;
  <span class="post-meta-item-icon">
    <i class="fa fa-folder-o"></i>
  </span>
  <span class="post-meta-item-text">分类：</span>
  
    <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
      <a href="http://www.shutdown.cn/categories/redis" itemprop="url" rel="index">
        <span itemprop="name">redis</span>
      </a>
      &nbsp; 
    </span>
  
    <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
      <a href="http://www.shutdown.cn/categories/redis-code" itemprop="url" rel="index">
        <span itemprop="name">redis-code</span>
      </a>
      &nbsp; 
    </span>
  
</span>


      
 <span>
&nbsp; | &nbsp;
<span class="post-meta-item-icon">
    <i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">转载</span>
</span>

       <span>
&nbsp; | &nbsp;
<span class="post-meta-item-icon">
    <i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读：</span>
<span class="leancloud-visitors-count">2660 字 ~6分钟</span>
</span>
      </div>
    </header>
    <div class="post-body" itemprop="articleBody">
    
    <div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback"> 文章系转载，便于整理和分类，原文地址：https://developer.aliyun.com/article/58703</code></pre></div>
    

    

<h2 id="前言">前言</h2>

<p>BIO顾名思义，<code>background IO</code>,是redis中运行的后台IO。 网上千篇一律的说法是redis是单线程单进程。 实际上redis运行过程中并不是严格单进程单线程应用。
Redis中的多进程：
在写入备份（RDB，AOF）的时候，会fork出子进程进行备份文件的写入。
Redis中的多线程：</p>

<ol>
<li>AOF的备份模式中，如果我们设置的是<code>AOF_FSYNC_EVERYSEC</code>（每秒备份一次，这个设置可理解为弱同步备份），redis会create一个backgroud线程，在这个线程中执行aof备份文件的写入。</li>
<li>新生成的AOF文件，在覆盖旧AOF文件时。 如果在此之前AOF备份已经开启，在执行该fd的close前，我们的Redis进程与旧的AOF文件存在引用， 旧的AOF文件不会真正被删除。 所以当我们执行close(oldfd)时，旧AOF文件的被打开该文件的进程数为0，即没有进程打开过这个文件，这时这个文件在执行close时会被真正删除。 而删除旧AOF文件可能会阻塞服务，所以我们将它放到另一个线程调用。</li>
<li>执行DEL操作，假如碰巧这个key对应有非常多对象，那么这个删除操作会阻塞服务器几秒钟时间， 所以将删除操作放到另一个线程执行。 具体可看这篇文章： <a href="http://antirez.com/news/93?spm=a2c6h.12873639.article-detail.6.45f545b5jp4a2k">Lazy Redis is better Redis</a>。</li>
</ol>

<h2 id="bio">BIO</h2>

<p>Redis将所有多线程操作封装到BIO中，在bio.c,bio.h中可以看到。 本文我们关注的不是具体的操作，而是Redis封装的BIO行为， 这个代码简洁，维护性好。 值得学习一下。
BIO提供以下几个api：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-c" data-lang="c"><span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">bioInit</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">void</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#8f5902;font-style:italic">//初始化BIO
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">bioCreateBackgroundJob</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">type</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">arg1</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">arg2</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">arg3</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>   <span style="color:#8f5902;font-style:italic">//新建一个BIO任务
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">unsigned</span> <span style="color:#204a87;font-weight:bold">long</span> <span style="color:#204a87;font-weight:bold">long</span> <span style="color:#000">bioPendingJobsOfType</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">type</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>  <span style="color:#8f5902;font-style:italic">//获取当前BIO任务类型，队列中待执行的任务个数
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">unsigned</span> <span style="color:#204a87;font-weight:bold">long</span> <span style="color:#204a87;font-weight:bold">long</span> <span style="color:#000">bioWaitStepOfType</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">type</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#8f5902;font-style:italic">//阻塞等待某个类型的BIO任务的执行，返回等待任务个数
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">bioKillThreads</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">void</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#ce5c00;font-weight:bold">/</span><span style="color:#ce5c00;font-weight:bold">/</span><span style="color:#a40000">中</span><span style="color:#a40000">断</span><span style="color:#a40000">所</span><span style="color:#a40000">有</span><span style="color:#000">BIO进程</span></code></pre></div>
<p>BIO操作的类型：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-c" data-lang="c"><span style="color:#8f5902;font-style:italic">/* Background job opcodes */</span>
<span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">define BIO_CLOSE_FILE    0 </span><span style="color:#8f5902;font-style:italic">/* 关闭文件*/</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">define BIO_AOF_FSYNC     1 </span><span style="color:#8f5902;font-style:italic">/* AOF写入  */</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">define BIO_LAZY_FREE     2 </span><span style="color:#8f5902;font-style:italic">/* 释放对象 */</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">define BIO_NUM_OPS       3 </span><span style="color:#8f5902;font-style:italic">/*BIO数*/</span></code></pre></div>
<p>BIO对象：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-c" data-lang="c"><span style="color:#204a87;font-weight:bold">static</span> <span style="color:#000">pthread_t</span> <span style="color:#000">bio_threads</span><span style="color:#000;font-weight:bold">[</span><span style="color:#000">BIO_NUM_OPS</span><span style="color:#000;font-weight:bold">]</span><span style="color:#000;font-weight:bold">;</span>  <span style="color:#8f5902;font-style:italic">//BIO线程
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">static</span> <span style="color:#000">pthread_mutex_t</span> <span style="color:#000">bio_mutex</span><span style="color:#000;font-weight:bold">[</span><span style="color:#000">BIO_NUM_OPS</span><span style="color:#000;font-weight:bold">]</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#8f5902;font-style:italic">//BIO每个线程的mutex锁变量
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">static</span> <span style="color:#000">pthread_cond_t</span> <span style="color:#000">bio_newjob_cond</span><span style="color:#000;font-weight:bold">[</span><span style="color:#000">BIO_NUM_OPS</span><span style="color:#000;font-weight:bold">]</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#8f5902;font-style:italic">//BIO线程锁的条件变量， 监听这个条件变量唤起当前线程
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">static</span> <span style="color:#000">pthread_cond_t</span> <span style="color:#000">bio_step_cond</span><span style="color:#000;font-weight:bold">[</span><span style="color:#000">BIO_NUM_OPS</span><span style="color:#000;font-weight:bold">]</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#8f5902;font-style:italic">//BIO线程阻塞锁，bioWaitStepOfType监听这个条件变量被通知该操作的执行。
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">static</span> <span style="color:#000">list</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">bio_jobs</span><span style="color:#000;font-weight:bold">[</span><span style="color:#000">BIO_NUM_OPS</span><span style="color:#000;font-weight:bold">]</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#204a87;font-weight:bold">static</span> <span style="color:#204a87;font-weight:bold">unsigned</span> <span style="color:#204a87;font-weight:bold">long</span> <span style="color:#204a87;font-weight:bold">long</span> <span style="color:#000">bio_pending</span><span style="color:#000;font-weight:bold">[</span><span style="color:#000">BIO_NUM_OPS</span><span style="color:#000;font-weight:bold">]</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#ce5c00;font-weight:bold">/</span><span style="color:#ce5c00;font-weight:bold">/</span> <span style="color:#000">BIO未执行的</span></code></pre></div>
<p>我们先看初始化的时候执行的部分：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-c" data-lang="c"><span style="color:#000">bioInit</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
    <span style="color:#204a87;font-weight:bold">for</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">j</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#000">j</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#000">BIO_NUM_OPS</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#000">j</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">arg</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">void</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">unsigned</span> <span style="color:#204a87;font-weight:bold">long</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000">j</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">pthread_create</span><span style="color:#000;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#204a87;font-weight:bold">thread</span><span style="color:#000;font-weight:bold">,</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">attr</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">bioProcessBackgroundJobs</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">arg</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">!</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span> <span style="color:#8f5902;font-style:italic">// 初始化线程
</span><span style="color:#8f5902;font-style:italic"></span>            <span style="color:#000">serverLog</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">LL_WARNING</span><span style="color:#000;font-weight:bold">,</span><span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">Fatal: Can&#39;t initialize Background Jobs.</span><span style="color:#4e9a06">&#34;</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#000">exit</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000;font-weight:bold">}</span>
        <span style="color:#000">bio_threads</span><span style="color:#000;font-weight:bold">[</span><span style="color:#000">j</span><span style="color:#000;font-weight:bold">]</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">thread</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000;font-weight:bold">}</span>
<span style="color:#000;font-weight:bold">}</span></code></pre></div>
<p>主要功能分为两个部分：</p>

<ol>
<li><code>bioCreateBackgroundJob</code>： 创建BIO任务，插入<code>bio_jobs</code>，并调用<code>pthread_cond_signal</code>，通知进程解锁。</li>
<li><code>bioProcessBackgroundJobs</code>： 执行BIO任务线程。 线程中通过<code>pthread</code>管理进程锁，当<code>bioCreateBackgroundJob</code>执行<code>pthread_cond_signal</code>通知到该任务对应的线程时，从<code>bio_jobs</code>读出上一个任务，并执行。</li>
</ol>

<h3 id="biocreatebackgroundjob">bioCreateBackgroundJob</h3>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">void bioCreateBackgroundJob(int type, void *arg1, void *arg2, void *arg3) {
    struct bio_job *job = zmalloc(sizeof(*job));
    job-&gt;time = time(NULL);
    job-&gt;arg1 = arg1;
    job-&gt;arg2 = arg2;
    job-&gt;arg3 = arg3;
    pthread_mutex_lock(&amp;bio_mutex[type]); // 加锁 保护bio_jobs和bio_pending的一致性
    listAddNodeTail(bio_jobs[type],job);  //插入到任务队列中
    bio_pending[type]++;  
    pthread_cond_signal(&amp;bio_newjob_cond[type]);  //通知preocess线程，执行任务
    pthread_mutex_unlock(&amp;bio_mutex[type]);  //解锁
}</code></pre></div>
<h3 id="bioprocessbackgroundjobs">bioProcessBackgroundJobs</h3>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">void *bioProcessBackgroundJobs(void *arg) {
    // 使进程可以被手动kill
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); 
    // 加锁 确保不会有两个进程使用pthread_cond_wait监听同一个锁
    pthread_mutex_lock(&amp;bio_mutex[type]);
    while(1) {
        listNode *ln;
        /* The loop always starts with the lock hold. */
        if (listLength(bio_jobs[type]) == 0) {
            pthread_cond_wait(&amp;bio_newjob_cond[type],&amp;bio_mutex[type]); // 等待bioCreateBackgroundJob通知解锁
            continue;
        }
        //取队列中第一个任务
        ln = listFirst(bio_jobs[type]);
        job = ln-&gt;value;
        /* It is now possible to unlock the background system as we know have
         * a stand alone job structure to process.*/
        pthread_mutex_unlock(&amp;bio_mutex[type]); //解锁
        // 根据type执行任务
        // do somethings...
        pthread_cond_broadcast(&amp;bio_step_cond[type]); // 广播解锁，用于解bioWaitStepOfType中的锁， 接触阻塞。
        pthread_mutex_lock(&amp;bio_mutex[type]); // 为下面的操作加锁，且用于下一个循环的pthread_cond_wait阻塞。
        listDelNode(bio_jobs[type],ln);  // 操作bio_jobs 和 bio_pending  标志这个任务已完成。
        bio_pending[type]--;
    }
}</code></pre></div>
<h3 id="pthread">pthread</h3>

<p>整个BIO就是通过锁进行的阻塞后台IO。 如果我们梳理一下这个锁过程：</p>

<ol>
<li><code>bioInit</code>,新建线程，执行<code>bioProcessBackgroundJobs</code>。</li>
<li><code>bioProcessBackgroundJobs</code> 中，<code>pthread_mutex_lock(&amp;bio_mutex[type])</code>，给该任务的锁变量加锁。</li>
<li>进入while循环， 调用<code>pthread_cond_wait</code>， 等待解锁。 由于<code>mutex</code>锁是“sleep-lock”，线程会sleep，等待唤醒。</li>
<li>主线程调用创建BIO任务， 调用<code>bioCreateBackgroundJob</code>。</li>
<li><code>bioCreateBackgroundJob</code>中<code>pthread_mutex_lock(&amp;bio_mutex[type]);</code>又对<code>bio_mutex[type]</code>加锁</li>
<li><code>bioCreateBackgroundJob</code>中<code>pthread_cond_signal(&amp;bio_newjob_cond[type])</code> //发送信号，通知BIO线程继续执行。</li>
<li><code>bioCreateBackgroundJob</code>中<code>pthread_mutex_unlock(&amp;bio_mutex[type]);</code>//解锁</li>
<li><code>bioProcessBackgroundJobs</code> 中被唤醒继续进行。</li>
<li>执行任务完毕后，<code>pthread_mutex_unlock</code>解锁， <code>pthread_cond_broadcast</code>广播解锁。</li>
<li>再<code>pthread_mutex_lock</code>加锁 。 用于下一次while循环。</li>
</ol>

<p>在梳理的时候，我发现一个奇怪的地方，我们第2步在BIO线程中加锁，第5步调用<code>bioCreateBackgroundJob</code>在主线程中又对<code>mutex</code>进行了一次加锁。 而在他们之间并没有<code>pthread_mutex_unlock</code>执行。 为什么<code>bioCreateBackgroundJob</code>没有被<code>mutex</code>的锁阻塞？
一切的关键都在<code>pthread_cond_wait</code>这个函数中。 按照我原来的理解，<code>pthread_cond_wait</code>应该只是进行了一次信号等待， 等到某个信号后，将<code>mutex[type]</code>解锁。 为什么在信号发送前，<code>pthread_mutex_lock</code>没有将主线程的<code>bioCreateBackgroundJob</code>阻塞住。 所以我猜测， <code>pthread_cond_wait</code>不不仅仅是一次<code>wait signal</code>，而是<code>unlock+wait</code>。
为了验证这个猜想，我们进去看pthread_cond_wait的实现:
<a href="https://github.com/lattera/glibc/blob/master/nptl/pthread_cond_wait.c#L127">glibc中的pthread_cond_wait</a></p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">// line 93
int __pthread_cond_wait (cond, mutex)
// line 110
  err = __pthread_mutex_unlock_usercnt (mutex, 0);  //解锁mutex
do {
// line 155    
lll_futex_wait (&amp;cond-&gt;__data.__futex, futex_val, pshared);  // wait signal
} while (val == seq || cond-&gt;__data.__woken_seq == val);
// line 193
return __pthread_mutex_cond_lock (mutex);</code></pre></div>
<p>可以看到， pthread_cond_wait 实际上就是一次 Unlock -&gt; Wait -&gt; Lock。</p>

<ol>
<li><a href="https://github.com/antirez/redis?spm=a2c6h.12873639.article-detail.8.45f545b5jp4a2k">Redis源码</a></li>
<li><a href="https://github.com/huangz1990/redis-3.0-annotated?spm=a2c6h.12873639.article-detail.9.45f545b5jp4a2k&amp;file=redis-3.0-annotated">Redis源码注释</a></li>
</ol>

    </div>
    <footer class="post-footer">
     
 
<div class="post-tags">     
     
    <a href="http://www.shutdown.cn/tags/redis" rel="tag" title="redis">#redis#</a>
    
    <a href="http://www.shutdown.cn/tags/bio" rel="tag" title="bio">#bio#</a>
    
    <a href="http://www.shutdown.cn/tags/thread" rel="tag" title="thread">#thread#</a>
    
    <a href="http://www.shutdown.cn/tags/%e6%ba%90%e7%a0%81" rel="tag" title="源码">#源码#</a>
    
</div>



     <div class="post-nav">
    <div class="post-nav-next post-nav-item">
    
        <a href="http://www.shutdown.cn/post/linux-c-condition-var-and-lock/" rel="next" title="条件变量与锁">
        <i class="fa fa-chevron-left"></i> 条件变量与锁
        </a>
    
    </div>

    <div class="post-nav-prev post-nav-item">
    
        <a href="http://www.shutdown.cn/post/redis-source-code-learn-of-total-sight/" rel="prev" title="Redis源码解析-全览">
        Redis源码解析-全览 <i class="fa fa-chevron-right"></i>
        </a>
    
    </div>
</div>
      
     
     
     






    </footer>
  </article>
</section>

          </div>
        </div>
        <div class="sidebar-toggle">
  <div class="sidebar-toggle-line-wrap">
    <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
  </div>
</div>
<aside id="sidebar" class="sidebar">
  <div class="sidebar-inner">

  <ul class="sidebar-nav motion-element">
    <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
      文章目录
    </li>
    <li class="sidebar-nav-overview" data-target="site-overview">
      站点概览
    </li>
  </ul>

    <section class="site-overview sidebar-panel ">
      <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image"
        src="http://www.shutdown.cn/img/author.jpg"
        alt="不与天斗Domino" />
    <p class="site-author-name" itemprop="name">不与天斗Domino</p>
    <p class="site-description motion-element" itemprop="description"> 
        Programmer &amp; Architect</p>
</div>
      <nav class="site-state motion-element">
    <div class="site-state-item site-state-posts">
      <a href="http://www.shutdown.cn/post/">
        <span class="site-state-item-count">183</span>
        <span class="site-state-item-name">日志</span>
      </a>
    </div>
    <div class="site-state-item site-state-categories">    
        <a href="http://www.shutdown.cn/categories/">      
         
        <span class="site-state-item-count">15</span>
        
        <span class="site-state-item-name">分类</span>
        
        </a>
    </div>

    <div class="site-state-item site-state-tags">
        <a href="http://www.shutdown.cn/tags/">
         
        <span class="site-state-item-count">224</span>
        
        <span class="site-state-item-name">标签</span>
        </a>
    </div>
</nav>
      
      

      

      <div class="links-of-blogroll motion-element inline">
<script type="text/javascript" src="//rf.revolvermaps.com/0/0/8.js?i=&amp;m=0&amp;s=220&amp;c=ff0000&amp;cr1=ffffff&amp;f=arial&amp;l=33&amp;bv=35" async="async"></script>
</div>

    </section>
    
<section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
    <div class="post-toc">
        <div class="post-toc-content"><nav id="TableOfContents">
<ul>
<li>
<ul>
<li><a href="#前言">前言</a></li>
<li><a href="#bio">BIO</a>
<ul>
<li><a href="#biocreatebackgroundjob">bioCreateBackgroundJob</a></li>
<li><a href="#bioprocessbackgroundjobs">bioProcessBackgroundJobs</a></li>
<li><a href="#pthread">pthread</a></li>
</ul></li>
</ul></li>
</ul>
</nav></div>
    </div>
</section>

  </div>
</aside>

      </div>
    </main>
   
    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  <span itemprop="copyrightYear">  &copy; 
  2013 - 2023</span>
  <span class="with-love"><i class="fa fa-heart"></i></span>
  <span class="author" itemprop="copyrightHolder">天地维杰网</span>
  <span class="icp" itemprop="copyrightHolder"><a href="https://beian.miit.gov.cn/" target="_blank">京ICP备13019191号-1</a></span>
</div>
<div class="powered-by">
  Powered by - <a class="theme-link" href="http://gohugo.io" target="_blank" title="hugo" >Hugo v0.63.2</a>
</div>
<div class="theme-info">
  Theme by - <a class="theme-link" href="https://github.com/xtfly/hugo-theme-next" target="_blank"> NexT
  </a>
</div>


      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
      <span id="scrollpercent"><span>0</span>%</span>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery/index.js?v=2.1.3"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/fastclick/lib/fastclick.min.js?v=1.0.6"></script> 
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.min.js?v=1.2.1"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script src="http://www.shutdown.cn/js/vendor/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>

<script src="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.pack.js?v=2.1.5"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/utils.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/motion.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/affix.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/schemes/pisces.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/scrollspy.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/post-details.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/toc.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/bootstrap.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js"],
    jax: ["input/TeX", "output/HTML-CSS"],
    tex2jax: {
      inlineMath: [ ['$','$'] ],
      displayMath: [ ['$$','$$'] ],
      processEscapes: true
    },
    "HTML-CSS": { fonts: ["TeX"] }
  });
</script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML' async></script>
</body>
</html>